// index.js
// 获取应用实例
const app = getApp()
const db = wx.cloud.database().collection('User')
const dbM = wx.cloud.database().collection('Mes_2')
Page({
  data: {
    count:0,
    openid: '',
    canIUseOpenData: true, // 如需尝试获取用户信息可改为false

    "vol_name": "姓名1",
    "vol_basedInfo": "学院专业年级1",

    info: []
  },



  //显示我的消息
  show_myNotice(){
      wx.navigateTo({
        url: '../notice/notice?id=' + this.data.openid + '&name=' + this.data.name
      })
  },

  //显示我的信息
  show_myInformation(){
    wx.navigateTo({
      url: '../vol_sel/vol_sel',
    })
  },

  //修改我的短信提醒信息
  show_sendSms() {
    wx.navigateTo({
      url: './phone/phone?id=' + this.data.openid
    })
  },

  //显示我的提问、我的回答、后台数据
  show_myQue() {

      wx.navigateTo({
        url: '../list/list?id=' + this.data.openid
      })
  },
  show_myAnswer() {

      wx.navigateTo({
        url: '../vol_lst/vol_list?id=' + this.data.openid + '&name=' + this.data.name
      })

  },
  show_bgData() {

      wx.navigateTo({
        url: '../bgData/bgData'
      })

  },
  show_data() {

    wx.navigateTo({
      url: '../mine/data/data'
    })

},

  onLoad() {
    wx.showLoading({
      title: '用户数据获取中',
      mask: true
    })
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    this.getOpenid();
  },

  onShow() {
    this.getOpenid();
  },

refresh:function(){
  dbM.where({
    id:this.data.openid,
    read:false
  }).count().then(res=>{
    this.setData({
      count:res.total
    })
  })
  
  db.where({
    _openid: this.data.openid
  }).get().then(res => {
    this.setData({
      info: res.data,
    })
    wx.hideLoading({})
  })
},

  gotocertification: function (param) {
      wx.navigateTo({
        url: 'certification',
      })
  },
  // 获取用户openid
  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log('云函数获取到的openid: ', res.result.openId)
        var openid = res.result.openId;
        wx.cloud.database().collection('User').where({
          _openid:openid
        }).get().then(res => {
          console.log(res.data[0])
          that.setData({
            name:res.data[0].name
          })
        })
        that.setData({
          openid: openid,
        })
        this.refresh()
      }
    })
  },
})